/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Consultar.java
 *
 * Created on 02/11/2011, 12:00:14
 */
package Interfaces;

import classes.Recurso;
import conexaoBD.mySQLcrud;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Kazuo
 */
public class TelaAdicionarRecursos extends javax.swing.JFrame {

    // private HashMap<Integer, Object> listaRecursos;
    private ArrayList<Recurso> arrayListRecursos;
    private mySQLcrud connectionCrud;
    private TelaCadastroMiniCurso formularioPai;
    private TelaExibicaoMiniCurso minicurso;
    private TelaExibicaoWorkshop workshop;
    private TelaExibicaoAtividadePalestra atividadePalestra;
    private TelaExibicaoAtividadePainel atividadePainel;


    //private ArrayList<String> listaRecursosSelecionados;

    /** Creates new form Consultar */
    public TelaAdicionarRecursos() {
        initComponents();
        connectionCrud = new mySQLcrud();
      //  listaRecursosSelecionados = new ArrayList<String>();
        arrayListRecursos = connectionCrud.listaRecursos("");
        preencheComboBoxRecurso();

    }

      public void setFormilario(TelaExibicaoWorkshop formPai) {
        workshop = formPai;
    }
    public void setFormilario(TelaCadastroMiniCurso formPai) {
        formularioPai = formPai;
    }
    public void setFormilario(TelaExibicaoMiniCurso mc) {
        minicurso = mc;
    }
    public void setFormilario(TelaExibicaoAtividadePalestra formPai) {
        atividadePalestra = formPai;
    }
    public void setFormilario(TelaExibicaoAtividadePainel mc) {
        atividadePainel = mc;
    }


    private void preencheComboBoxRecurso() {

        DefaultComboBoxModel modelListaRecursos = new DefaultComboBoxModel();
        modelListaRecursos.addElement("Selecione o tipo do recurso");
        for (int i = 0; i < arrayListRecursos.size(); i++) {
            String tipoRecurso = arrayListRecursos.get(i).getTipoRecurso();
            if (modelListaRecursos.getIndexOf(tipoRecurso) == -1) {
                modelListaRecursos.addElement(tipoRecurso);
            }
        }
        cbSelecionaTipoRecurso.setModel(modelListaRecursos);
    }

    private void preecheListaRecursos(String itemSelecionado) {
        jListRecursos.removeAll();
        DefaultListModel modelListaRecursos = new DefaultListModel();

        for (int i = 0; i < arrayListRecursos.size(); i++) {
            String tipoRecurso = arrayListRecursos.get(i).getTipoRecurso();
            if (tipoRecurso.equals(itemSelecionado)) {
                modelListaRecursos.addElement(arrayListRecursos.get(i).getNomeRecurso());
            }
        }

        jListRecursos.setModel(modelListaRecursos);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblTitulo = new javax.swing.JLabel();
        lblParamConsulta = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btConfirmar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListRecursos = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListRecursosAdicionados = new javax.swing.JList();
        btCancelar = new javax.swing.JButton();
        cbSelecionaTipoRecurso = new javax.swing.JComboBox();
        btRemover = new javax.swing.JToggleButton();
        btAdicionar = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblTitulo.setText("Adicionar");
        jPanel1.add(lblTitulo);

        lblParamConsulta.setFont(new java.awt.Font("Tahoma", 1, 16));
        lblParamConsulta.setText("Recursos");
        jPanel1.add(lblParamConsulta);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        btConfirmar.setText("Confirmar");
        btConfirmar.setToolTipText("Confirmar Cadastro de Sessão");
        btConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(34, 0, 0, 0);
        jPanel2.add(btConfirmar, gridBagConstraints);

        jScrollPane1.setViewportView(jListRecursos);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 220;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanel2.add(jScrollPane1, gridBagConstraints);

        jScrollPane2.setViewportView(jListRecursosAdicionados);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 220;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel2.add(jScrollPane2, gridBagConstraints);

        btCancelar.setText("Cancelar");
        btCancelar.setToolTipText("Cancelar e retornar para a tela anterior");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(34, 0, 0, 0);
        jPanel2.add(btCancelar, gridBagConstraints);

        cbSelecionaTipoRecurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione o tipo do" }));
        cbSelecionaTipoRecurso.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbSelecionaTipoRecursoItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanel2.add(cbSelecionaTipoRecurso, gridBagConstraints);

        btRemover.setText("<< Remover");
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        jPanel2.add(btRemover, gridBagConstraints);

        btAdicionar.setText("Adicionar >>");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        jPanel2.add(btAdicionar, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmarActionPerformed
        ArrayList<String> listaRecursosString = new ArrayList<String>();
        ArrayList<Recurso> listaRecursosObj = new ArrayList<Recurso>();


        for (int i = 0; i < jListRecursosAdicionados.getModel().getSize(); i++) {
            listaRecursosString.add((String) jListRecursosAdicionados.getModel().getElementAt(i));
        }

        for (Recurso r : arrayListRecursos) {
            if (listaRecursosString.contains(r.getNomeRecurso())) {
                listaRecursosObj.add(r);
            }
        }

        formularioPai.setListaRecursosSelecionados(listaRecursosObj);
        formularioPai = null;

        this.dispose();
    }//GEN-LAST:event_btConfirmarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void cbSelecionaTipoRecursoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecionaTipoRecursoItemStateChanged

        String itemSelecionado = (String) cbSelecionaTipoRecurso.getSelectedItem();
        if (!itemSelecionado.equals("Selecione o tipo do recurso")) {
            preecheListaRecursos(itemSelecionado);
        }
}//GEN-LAST:event_cbSelecionaTipoRecursoItemStateChanged

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        Object[] objItensSelecionados = jListRecursos.getSelectedValues();
        ArrayList<String> itemNaoSelecionado = new ArrayList<String>();
        ArrayList<String> itemSelecionado = new ArrayList<String>();
        DefaultListModel listaTotal = new DefaultListModel();

        if (objItensSelecionados.length >0) {
            for (int i = 0; i < jListRecursos.getModel().getSize(); i++) {
                itemNaoSelecionado.add((String) jListRecursos.getModel().getElementAt(i));
            }


            for (int i = 0; i < jListRecursosAdicionados.getModel().getSize(); i++) {
                itemSelecionado.add((String) jListRecursosAdicionados.getModel().getElementAt(i));
            }

            if (objItensSelecionados.length > 0) {
                for (int i = 0; i < objItensSelecionados.length; i++) {
                    if (!itemSelecionado.contains((String) objItensSelecionados[i])) {
                        itemSelecionado.add((String) objItensSelecionados[i]);
                    } else {
                        JOptionPane.showMessageDialog(null, "O recurso '" + (String) objItensSelecionados[i] + "' já foi adicionado a lista de recursos selecionados");
                    }
                }
            } else {
                if (!itemSelecionado.contains((String) objItensSelecionados[0])) {
                    itemSelecionado.add((String) objItensSelecionados[0]);
                } else {
                    JOptionPane.showMessageDialog(null, "O recurso '" + (String) objItensSelecionados[0] + "' já foi adicionado a lista de recursos selecionados");
                }
            }


            for (String s : itemSelecionado) {
                listaTotal.addElement(s);
            }

            jListRecursosAdicionados.setModel(listaTotal);
        } else {
            JOptionPane.showMessageDialog(null, "Você deve selecionar algum recurso para adiciona-lo");
        }
}//GEN-LAST:event_btAdicionarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        Object[] objListaRecursosAdicionadosSelecionado = jListRecursosAdicionados.getSelectedValues();
        ArrayList<String> listaTodosNomes = new ArrayList<String>();
        ArrayList<String> arrayRecursosAdicionadosSelecionado = new ArrayList<String>();
        ArrayList<String> interseccaoRecursosAdicionadosRemovido = new ArrayList<String>();

        if(objListaRecursosAdicionadosSelecionado.length >0){

            // tranforma os elementos de Object[] p/ arrayList<string>
            for (Object o : objListaRecursosAdicionadosSelecionado) {
                arrayRecursosAdicionadosSelecionado.add((String) o);
            }

            // lista todos os nomes dos recursos
            for (int i = 0; i < jListRecursosAdicionados.getModel().getSize(); i++) {
                listaTodosNomes.add((String) jListRecursosAdicionados.getModel().getElementAt(i));
            }

            // compara e add
            for (String s : listaTodosNomes) {
                if (!arrayRecursosAdicionadosSelecionado.contains(s)) {
                    interseccaoRecursosAdicionadosRemovido.add(s);
                }
            }

            DefaultListModel modelList = new DefaultListModel();

            for (String s : interseccaoRecursosAdicionadosRemovido) {
                modelList.addElement(s);
            }

            jListRecursosAdicionados.setModel(modelList);
        }else{
            JOptionPane.showMessageDialog(null, "Você deve selecionar algum recurso para remove-lo");
        }
}//GEN-LAST:event_btRemoverActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaConsulta().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btAdicionar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btConfirmar;
    private javax.swing.JToggleButton btRemover;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbSelecionaTipoRecurso;
    private javax.swing.JList jListRecursos;
    private javax.swing.JList jListRecursosAdicionados;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblParamConsulta;
    private javax.swing.JLabel lblTitulo;
    // End of variables declaration//GEN-END:variables
}
